home *** CD-ROM | disk | FTP | other *** search
- Path: damon.irf.uni.dortmund.de!broth
- From: rothert@damon.irf.uni-dortmund.de (Bernd Rothert)
- Newsgroups: comp.lang.c++
- Subject: Re: Borland C++ 4.52 - problems with tellg & ftell
- Date: Wed, 24 Jan 96 12:45:21 GMT
- Organization: Institute of Robotics Research
- Message-ID: <4e59r8$bb4@damon.irf.uni-dortmund.de>
- References: <4dk1hm$se7@kettle.magna.com.au>
- NNTP-Posting-Host: broth.irf
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4dk1hm$se7@kettle.magna.com.au>,
- techteam@sratem.com.au (Technical Team) wrote:
- >PROBLEM WITH istream:: tellg() using Borland C++ 4.52 WIN 32
- >Has anyone else had problems using tellg or ftell using Borland with
- >WIN 32 ?
- Yes - I also had problems parsing a DOS text file using fstreams because
- tellg() modified the current stream position (BC++4.5 and MSVC++1.51).
-
- Stream handling and especially tellg() seems to be a problem for most DOS C++
- Compilers because of the special handling of the CR/LF sequence in text mode.
-
- The fstream classes open files in text mode by default. So each CR/LF pair
- should count as one '\n' (char 10) as it is when reading the file one
- character at a time. Therefore tellg() should return the number of characters
- from the beginning of the file with CR/LF = 1 char, not the physical position.
-
- Try the following small program - it fails on most DOS compilers UNLESS YOU
- USE ios::binary IN THE CTOR OF THE ifstream (commented out below).
- The bad news is that you have to handle CR/LF on your own in binary mode.
-
- ---cut here---
- #include <fstream.h>
- const char FNTEST[] = "test.tmp";
- const char DIGITS[] = "0123456789";
- void digits(ostream& strm, int n) {
- for (int i = 0; i < n; i++)
- strm << DIGITS[i % 10];
- strm << endl;
- }
- int main(int argc, char*[]) {
- if (argc < 2)
- digits(ofstream(FNTEST), 512); // create text mode file with CR/LF
- else
- digits(ofstream(FNTEST, ios::binary), 512); // binary/Un*x file with LF
- digits(cout, 10);
- ifstream istrm(FNTEST /*, ios::binary */); // remove comment to make it work
- for (int i = 0; i < 10; i++) {
- istrm.tellg();
- cout << (char)istrm.get();
- }
- return 0;
- }
- /* Results (option "-ml"):
- CR/LF DOS Text Un*x LF only
- BC++ 4.5 0135791357 0123456789 (ok)
- BC++ 3.1 0123456789 (ok) 0111111111
- MSVC++ 1.51 0134567890 0111111111
- MSVC++ 2.2 0134567890 0111111111
- WATCOM 10.5 0123456789 (ok) 0111111111
- */
- ---cut here---
-
-